//
//  TrickTenderShaiXuan.m
//  HappyStore
//
//  Created by 李小辉 on 16/7/7.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "TrickTenderShaiXuan.h"

@interface TrickTenderShaiXuan ()<selectedSingleRowDelegate>
@property (strong, nonatomic) NSArray *priceAry;
@property (strong, nonatomic) NSArray *projectTypeAry;
@property (strong, nonatomic) NSArray *addressAry;
@property (strong, nonatomic) NSArray *numbersAry;
@property (strong, nonatomic) SingleSelectedDialog *numbersSingleSelected;

@property (strong, nonatomic) UIButton *projectTypeBtn;
@property (strong, nonatomic) UIButton *priceBtn;
@property (strong, nonatomic) UIButton *addressBtn;
@property (strong, nonatomic) NSString *selectedNumStr;
@property (strong, nonatomic) UITextField *numberTextField;
@end

@implementation TrickTenderShaiXuan
- (NSArray *)numbersAry{
    if (!_numbersAry) {
        _numbersAry = @[@"0~1000",@"1000~3000",@"3000~5000",@"5000以上"];
    }
    return _numbersAry;
}
-(NSArray *)projectTypeAry{
    if (!_projectTypeAry) {
        
        _projectTypeAry = @[@"工程建设",@"政府采购",@"矿产权",@"国有产权",@"土地出让",@"公车拍卖"];
    }
    return _projectTypeAry;
}
-(NSArray *)priceAry{
    if (!_priceAry) {
        
        _priceAry = @[@"不限",@"50万以下",@"50-100万",@"100-500万",@"500-1000万",@"1000-5000万",@"5000万－1亿",@"1-10亿",@"10亿以上"];
    }
    
    return _priceAry;
}
-(NSArray *)addressAry{
    
    if (!_addressAry) {
        _addressAry = @[@"北京",@"天津",@"河北",@"内蒙古",@"山西",@"黑龙江",@"吉林",@"辽宁",@"山东",@"安徽",@"江苏",@"上海",@"浙江",@"江西",@"福建",@"河南",@"湖北",@"湖南",@"广西",@"广东",@"海南",@"四川",@"云南",@"贵州",@"重庆",@"西藏",@"陕西",@"甘肃",@"宁夏",@"山西",@"新疆",@"青海",@"香港",@"澳门",@"台湾"];
    }
    
    return _addressAry;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"筛选";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;

    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(0, screenH + 300);
    [self addIndustryButton];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
//返回
- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 点确定
- (void)rightBarButtonItemClick{
    NSMutableDictionary *dicInfo = [NSMutableDictionary dictionary];
    [dicInfo setObject:@"1" forKey:@"page"];
    if (self.addressBtn.selected) {//地区
        [dicInfo setObject:self.addressBtn.titleLabel.text forKey:@"address"];
    }
    if ([self.supplyChainStr isEqualToString:@"供应链"]) {
        
//        @"0~1000",@"1000~3000",@"3000~5000",@"5000以上"
        if ([self.selectedNumStr isEqualToString:@"0~1000"]) {
            [dicInfo setObject:@"0" forKey:@"startNumber"];
            [dicInfo setObject:@"1000" forKey:@"endNumber"];
        }else  if ([self.selectedNumStr isEqualToString:@"1000~3000"]) {
            [dicInfo setObject:@"1000" forKey:@"startNumber"];
            [dicInfo setObject:@"3000" forKey:@"endNumber"];
        }else if ([self.selectedNumStr isEqualToString:@"3000~5000"]) {
            [dicInfo setObject:@"3000" forKey:@"startNumber"];
            [dicInfo setObject:@"5000" forKey:@"endNumber"];
        }else {
            [dicInfo setObject:@"5000" forKey:@"startNumber"];
    
        }
    }else{//招投标
        if (self.projectTypeBtn.selected) {//projectType=项目类型
            [dicInfo setObject:self.projectTypeBtn.titleLabel.text forKey:@"projectType"];
        }
        if (self.priceBtn.selected) {//根据金额
            for (int i = 0; i<self.priceAry.count; i++) {
                if ([self.priceBtn.titleLabel.text isEqualToString:self.priceAry[i]]) {
                    //@"0-500万",@"500万-3000万",@"3000万-1亿",@"1亿以上"
                    switch (i) {
                            
                        case 0:
                        {
                            [dicInfo setObject:@"" forKey:@"startMoney"];
                        }
                            break;
                        case 1:
                        {
                            [dicInfo setObject:@"0-5000000" forKey:@"startMoney"];
                        }
                            break;
                        case 2:
                        {
                            [dicInfo setObject:@"500000-1000000" forKey:@"startMoney"];
                        }
                            break;
                        case 3:
                        {
                            [dicInfo setObject:@"1000000-5000000" forKey:@"startMoney"];
                        }
                            break;
                        case 4:
                        {
                            [dicInfo setObject:@"5000000-10000000" forKey:@"startMoney"];
                        }
                            break;
                        case 5:
                        {
                            [dicInfo setObject:@"10000000-50000000" forKey:@"startMoney"];
                        }
                            break;
                        case 6:
                        {
                            [dicInfo setObject:@"50000000-100000000" forKey:@"startMoney"];
                        }
                            break;
                        case 7:
                        {
                            [dicInfo setObject:@"100000000-1000000000" forKey:@"startMoney"];
                        }
                            break;
                        case 8:
                        {
                            [dicInfo setObject:@"1000000000－亿" forKey:@"startMoney"];
                        }
                            break;
                            
                    }
                }
            }
            
        }
        
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TrickTenderShaiXuan" object:nil userInfo:dicInfo];
    [self.navigationController popViewControllerAnimated:YES];

}
-(void)addIndustryButton{
    
    NSMutableArray *industryArr = [NSMutableArray array];
    NSMutableArray *priceArr = [NSMutableArray array];
    NSMutableArray *diQuArr = [NSMutableArray array];
    if ([self.supplyChainStr isEqualToString:@"供应链"]) {
        UITextField *textFieldNum = [[UITextField alloc] initWithFrame:CGRectMake(12, 30, screenW-24, 30)];
        textFieldNum.tag = 301;
        textFieldNum.placeholder = @"选择产品数量";
        textFieldNum.userInteractionEnabled = NO;
        textFieldNum.borderStyle = UITextBorderStyleRoundedRect;
        self.numberTextField = textFieldNum;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(12, 30, screenW-24, 30)];
        [btn addTarget:self action:@selector(clickNumbers) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:textFieldNum];
        [self.scrollView addSubview:btn];
        [priceArr addObject:btn];
        
    }else{//招投标
        UILabel *industry = [[UILabel alloc]initWithFrame:CGRectMake(10, 18, 100, 30)];
        industry.text = @"项目类型";
        industry.textColor = [UIColor blackColor];
        industry.font = [UIFont systemFontOfSize:16];
        [self.scrollView addSubview:industry];
        
        for (int i = 0 ; i < self.projectTypeAry.count; i++) {
            
            //创建按钮
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*102+12, i/3*40 + industry.y + industry.height + 8 , 92, 30)];
            [button setTitle:self.projectTypeAry[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            
            [button addTarget:self action:@selector(btnClickHangYe:) forControlEvents:UIControlEventTouchUpInside];
            button.layer.masksToBounds = YES;
            button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
            button.layer.borderWidth = 1.0f;
            button.layer.cornerRadius = 5.0f;
            [industryArr addObject:button];
            
            [self.scrollView addSubview:button];
        }
        
        UIButton *hangYebtn = industryArr.lastObject;
        
        UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(10, hangYebtn.height + hangYebtn.y + 20, 100, 30)];
        price.text = @"金额区间";
        price.textColor = [UIColor blackColor];
        price.font = [UIFont systemFontOfSize:16];
        [self.scrollView addSubview:price];
        
        for (int i = 0; i < self.priceAry.count; i++) {
            
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*102+12, i/3*40+price.y + price.height + 8, 92, 30)];
            [button setTitle:self.priceAry[i] forState:UIControlStateNormal];
            
            [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            if (i ==4||i == 5) {
                button.titleLabel.font =[UIFont systemFontOfSize:11.f];
            }
            [button addTarget:self action:@selector(btnClickJinE:) forControlEvents:UIControlEventTouchUpInside];
            button.layer.masksToBounds = YES;
            button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
            button.layer.borderWidth = 1.0f;
            button.layer.cornerRadius = 5.0f;
            [priceArr addObject:button];
            
            [self.scrollView addSubview:button];
        }
    }
    //选择地区
    UIButton *jinEbtn = priceArr.lastObject;
    
    UILabel *address = [[UILabel alloc]initWithFrame:CGRectMake(10, jinEbtn.height + jinEbtn.y + 20, 100, 30)];
    address.text = @"选择地区";
    address.textColor = [UIColor blackColor];
    address.font = [UIFont systemFontOfSize:16];
    [self.scrollView addSubview:address];
    
    for (int i = 0 ; i < self.addressAry.count; i++) {
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*102+12, i/3*40+address.y + address.height + 8, 92, 30)];
       
        [button setTitle:self.addressAry[i] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [button addTarget:self action:@selector(btnClickAddress:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        button.layer.borderWidth = 1.0f;
        button.layer.cornerRadius = 5.0f;
        [diQuArr addObject:button];
        
        [self.scrollView addSubview:button];
    }
}
#pragma mark - 点击按钮
-(void)btnClickHangYe:(UIButton *)btn{
    
    if (btn.selected) {
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (self.projectTypeBtn != nil) {
            self.projectTypeBtn.selected = NO;
            [self.projectTypeBtn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            self.projectTypeBtn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        self.projectTypeBtn = btn;
    }
}

-(void)btnClickJinE:(UIButton *)btn{
    if (btn.selected) {
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (self.priceBtn != nil) {
            self.priceBtn.selected = NO;
            [self.priceBtn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            self.priceBtn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        self.priceBtn = btn;
    }
}

-(void)btnClickAddress:(UIButton *)btn{
    
    if (btn.selected) {
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (self.addressBtn != nil) {
            self.addressBtn.selected = NO;
            [self.addressBtn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            self.addressBtn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        self.addressBtn = btn;
    }
    
}
- (void)clickNumbers{
    self.numbersSingleSelected = [SingleSelectedDialog sharedSelectedAddress];
    self.numbersSingleSelected.delegatee = self;
    NSArray *rowAry = [NSArray arrayWithArray:self.numbersAry];

    [self.numbersSingleSelected btnClickAddressWithData:rowAry];
}

- (void)seceltedRow:(NSInteger)rrow
{
    self.selectedNumStr = self.numbersAry[rrow];
    self.numberTextField.text = self.numbersAry[rrow];
}


@end
